<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 头部操作栏 -->
            <div class="layui-form lay-header">
                <div class="toolbar">
                    <div class="layui-form-item lay-query-content">
                        <div class="lay-query-box">
                            <div class="query-lt">
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">名称：</label>
                                    <div class="layui-input-inline">
                                        <input name="name" class="layui-input" type="text" placeholder="请输入名称"/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <button class="layui-btn icon-btn color-green" lay-filter="formSubSearchLog" lay-submit>查询</button>
                                    <button class="layui-btn icon-btn color-white" lay-filter="resets" lay-submit>重置</button>
                                </div>
                            </div>
                            <div class="query-rt"></div>
                        </div>
                    </div>
                </div>
                <div class="lay-btn-rows">
                    <div class="lay-btn-box">
                        <div class="lay-btn-lt">
                            <button id="customerComplaintTypeBtnBatchDel" class="layui-btn icon-btn layui-btn-danger layui-disabled" disabled >批量删除</button>
                            <button id="customerComplaintTypeBtnAdd" class="layui-btn icon-btn color-reseda">添加</button>
                        </div>
                        <div class="lay-btn-rt">
                        </div>
                    </div>
                </div>
            </div>
            <!-- 表格 -->
            <table class="layui-table" id="customerComplaintTypeTable" lay-filter="customerComplaintTypeTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="customerComplaintTypeTableBar">
    <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="customerComplaintTypeForm">
    <form lay-filter="customerComplaintTypeForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label">名称</label>
            <div class="layui-input-block">
                <input name="name" placeholder="请输入名称" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">编号</label>
            <div class="layui-input-block">
                <input name="code" placeholder="请输入编号" type="text" class="layui-input" lay-verify="required|isOnly" required/>
            </div>
        </div>
        <div class="layui-form-item" >
            <label class="layui-form-label">描述</label>
            <div class="layui-input-block">
                <textarea id="remark" name="remark" placeholder="请输入描述" class="layui-textarea" />
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">是否启用</label>
            <div class="layui-input-block">
                <input name="isDeleted" type="checkbox" lay-skin="switch" value="1" checked="true" lay-text="启用|停用"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">排序</label>
            <div class="layui-input-block">
                <input name="sortNumber" placeholder="请输入排序" type="text" class="layui-input" />
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="customerComplaintTypeFormSubmit" lay-submit>保存</button>
        </div>
    </form>
</script>

<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'admin'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var admin = layui.admin;
        var upload = layui.upload;

        // 渲染表格
        var insTb = table.render({
            elem: '#customerComplaintTypeTable',
            url: config.base + 'enterprise/sell/admin/customerComplaintType/list',
            toolbar: true,
            defaultToolbar: ["filter"],
            page: true,
            cellMinWidth: 100,
            cols: [[
                {type: 'checkbox'}
                ,{field: 'name', align: 'center', title: '名称'}
                ,{field: 'code', align: 'center', title: '编号'}
                ,{field: 'remark', align: 'center', title: '描述'}
                ,{field: 'isDeleted', align: 'center', title: '是否启用', templet:function (d) {
                        if(d.isDeleted){
                            return '<input name="isDeleted" type="checkbox" lay-skin="switch" value="'+d.id+'" checked="'+d.isDeleted+'" lay-filter="isDeleted"  lay-text="启用|停用"/>';
                        }else{
                            return '<input name="isDeleted" type="checkbox" lay-skin="switch" value="'+d.id+'" lay-filter="isDeleted"   lay-text="启用|停用"/>';
                        }
                    }}
                ,{field: 'createTime', align: 'center', title: '创建时间'}
                ,{align: 'center', toolbar: '#customerComplaintTypeTableBar', title: '操作', width: 200}
            ]],
            done: function (res, curr, count) {
                //移除按钮
                var menuButton = config.getMenuButton(location.hash);
                if(menuButton.indexOf("搜索") == -1){
                    $('.layui-form.toolbar').remove();
                }
                if(menuButton.indexOf("修改") == -1){
                    $("[lay-event='edit']").remove();
                }
                if(menuButton.indexOf("删除") == -1){
                    $("[lay-event='del']").remove();
                }
            }
        });

        // 工具条点击事件
        table.on('tool(customerComplaintTypeTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') { //修改
                showEditModel(data);
            } else if (obj.event === 'del') { // 删除
                doDelete(obj);
            }
        });

        //监听排序
        table.on('sort(customerComplaintTypeTable)', function(obj){
            table.reload('customerComplaintTypeTable', {
                initSort: obj
                ,where: {
                    sort: obj.field
                    ,order: obj.type
                }
            });
        });

        // 搜索
        form.on('submit(formSubSearchLog)', function (data) {
            insTb.reload({where: data.field,page: {curr: 1}},'data');
        });

        // 重置搜索
        form.on('submit(resets)', data => {
            $(".lay-header").find('select').val("");
            $(".lay-header").find('input').val("");
            let field = data.field;
            for (let key in field) {
                field[key] = ''
            }
            insTb.reload({where: field, page: {curr: 1}}, 'data');
        });

        form.on('switch(isDeleted)',function(obj){
            if(obj.elem.checked){
                enable(obj.value)
            }else{
                disable(obj.value)
            }
        })

        //是否启用
        function enable(id){
            admin.req('enterprise/sell/admin/customerComplaintType/enable', {id:id}, function (res) {
                if (res.code == 200) {
                    layer.msg(res.msg, {icon: 1});
                    insTb.reload('customerComplaintTypeTable');
                } else {
                    layer.msg(res.msg, {icon: 2});
                }
            }, 'GET');
        }

        //是否停用
        function disable(id){
            admin.req('enterprise/sell/admin/customerComplaintType/disable', {id:id}, function (res) {
                if (res.code == 200) {
                    layer.msg(res.msg, {icon: 1});
                    insTb.reload('customerComplaintTypeTable');
                } else {
                    layer.msg(res.msg, {icon: 2});
                }
            }, 'GET');
        }

        // 监听表格checkbox
        table.on('checkbox(customerComplaintTypeTable)', function (obj) {
            // console.log(obj.checked); // 当前是否选中
            // console.log(obj.data); // 选中行的相关数据
            // console.log(obj.type); // 全选：all  单选：one
            // console.log(table.checkStatus('customerComplaintTypeTable').data); // 获取表格中选中行的数据
            var dataS = table.checkStatus('customerComplaintTypeTable').data;
            if(dataS.length > 0){
                $('#customerComplaintTypeBtnBatchDel').removeClass("layui-disabled").removeAttr("disabled");
            }else{
                $('#customerComplaintTypeBtnBatchDel').addClass("layui-disabled").attr("disabled", true);
            }
        })

        // 批量删除
        $('#customerComplaintTypeBtnBatchDel').click(function () {
            // 获取选中的id
            var ids = "";
            var checkStatus = table.checkStatus('customerComplaintTypeTable'); //test即为基础参数id对应的值
            var dataS = checkStatus.data;
            for (var i = 0; i < dataS.length; i++) {
                if(i == 0){
                    ids = dataS[i].id
                }else{
                    ids += "," + dataS[i].id
                }
            }
            layer.confirm('确定要批量删除吗？', {
                offset: '65px',
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('enterprise/sell/admin/customerComplaintType/batchDelete', {ids: ids}, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                        table.reload('customerComplaintTypeTable');
                        $('#customerComplaintTypeBtnBatchDel').addClass("layui-disabled").attr("disabled", true);
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'GET');
            });

        });

        // 添加按钮点击事件
        $('#customerComplaintTypeBtnAdd').click(function () {
            showEditModel();
        });

        // 显示编辑弹窗
        function showEditModel(data) {
            admin.open({
                type: 1,
                area: '640px',
                offset: '65px',
                title: data ? '修改客诉类型' : '添加客诉类型',
                content: $('#customerComplaintTypeForm').html(),
                success: function () {
                    //自定义验证规则
                    form.verify({
                        isOnly: function (value) {
                            var isOnly = true;
                            var idVal = $("form input[name='id']").val();
                            admin.req('enterprise/sell/admin/customerComplaintType/isOnly', {code:value,id:idVal}, function (res) {
                                if (res.code == 200) {
                                    isOnly = res.data;
                                }
                            }, 'GET',false);
                            if(!isOnly){
                                return "编号必须唯一"
                            }
                        }
                    })

                    form.val('customerComplaintTypeForm', data);
                    // 表单提交事件
                    form.on('submit(customerComplaintTypeFormSubmit)', function (d) {
                        layer.load(2);
                        var isDeleted=$("form input[name='isDeleted']").prop("checked");
                        if(!isDeleted){
                            d.field.isDeleted=false;
                        }else{
                            d.field.isDeleted=true;
                        }
                        admin.req(data ? 'enterprise/sell/admin/customerComplaintType/update' : 'enterprise/sell/admin/customerComplaintType/add', d.field, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('customerComplaintTypeTable');
                                layer.closeAll('page');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, data ? 'PUT' : 'POST');
                        return false;
                    });
                }
            });
        }

        // 删除
        function doDelete(obj) {
            layer.confirm('确定要删除吗？', {
                offset: '65px',
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('enterprise/sell/admin/customerComplaintType/' + obj.data.id, {}, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                        obj.del();
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'DELETE');
            });
        }

        // 导出excel
        $('#customerComplaintTypeBtnExport').click(function () {
            var checkRows = table.checkStatus('customerComplaintTypeTable');
            if (checkRows.data.length == 0) {
                layer.msg('请选择要导出的数据', {icon: 2});
            } else {
                table.exportFile(insTb.config.id, checkRows.data, 'xls');
            }
        });

    });
</script>